﻿using System.Collections;
using System.Collections.Generic;
using Client.UI;
using GameFramework;
using GameFramework.DataTable;
using GameFramework.Event;
using GameFramework.Fsm;
using GameFramework.Procedure;
using GameFrameworkExample;
using JetBrains.Annotations;
using UnityEngine;
using UnityGameFramework.Runtime;

namespace Client {
    public class ProcedureMain : ProcedureBase {
        private bool m_StartGame = false;
        private StartGameDialog m_MenuForm = null;
        protected override void OnInit(IFsm<IProcedureManager> procedureOwner) {
            base.OnInit(procedureOwner);
        }

        protected override void OnEnter(IFsm<IProcedureManager> procedureOwner) {
            base.OnEnter(procedureOwner);
            // 加载界面
            GameEntry.Event.Subscribe(OpenUIFormSuccessEventArgs.EventId,OnOpenUIFormSuccess);
            GameEntry.UI.OpenUIForm(UIFormId.MainMenu, this);
        }

        protected override void OnUpdate(IFsm<IProcedureManager> procedureOwner, float elapseSeconds, float realElapseSeconds) {
            base.OnUpdate(procedureOwner, elapseSeconds, realElapseSeconds);
            if (m_StartGame) {
                ChangeState<ProcedureExample>(procedureOwner);
            }
        }

        protected override void OnLeave(IFsm<IProcedureManager> procedureOwner, bool isShutdown) {
            base.OnLeave(procedureOwner, isShutdown);
            GameEntry.Event.Unsubscribe(OpenUIFormSuccessEventArgs.EventId,OnOpenUIFormSuccess);
            GameEntry.UI.CloseUIForm(m_MenuForm);
        }
        
        private void OnOpenUIFormSuccess(object sender, GameEventArgs e)
        {
            OpenUIFormSuccessEventArgs ne = (OpenUIFormSuccessEventArgs)e;
            // 判断userData是否为自己
            if (ne.UserData != this)
            {
                return;
            }
            Log.Debug("UI_Menu：恭喜你，成功地召唤了我。");
            m_MenuForm = ne.UIForm.Logic as StartGameDialog;
            //TestLoadDateTable();
        }

        /// <summary>
        /// 测试加载表格内容
        /// </summary>
        private void TestLoadDateTable() {
            IDataTable<DRTestDataTable> dtTestDateTable = GameEntry.DataTable.GetDataTable<DRTestDataTable>();
            DRTestDataTable drTestDataTable = dtTestDateTable.GetDataRow(10000);
            if (drTestDataTable == null) {
                Log.Warning("Can not load entity id '{0}' form data table!",0);
                return;
            }
        }

        /// <summary>
        /// 测试开始游戏方法
        /// </summary>
        public void StartGame() {
            m_StartGame = true;
        }
    }
}
